﻿/// <reference path="../../Scripts/jquery-1.4.1-vsdoc.js" />
/// <reference path="../../Content/Scripts/Common.js" />

//this is the model array. Its item's indeces must be as same as the row indeces in the "#materialTable";
var _materials = [];
//id = m.Id,
//name = m.Name,
//amount = 1,
//image = Url.Content(m.ImagePath),
//barcode = barcode,

$(function () {
	$("#materialTable").hide();

	//this method will be invoked after barcode is input.
	addBarcodeReadyHandler(function (barcode) {
		_removeAllOrders();

		//check if its corresponding material is exsit already.
		for (var i = 0; i < _materials.length; i++) {
			var m = _materials[i];
			if (m.barcode == barcode) {
				//if it exists, add the model's amount.
				m.amount += 1;
				//i is its row index too.
				var amountTxt = $("#Amount_" + i);
				amountTxt.val(m.amount).effect("pulsate");
				return;
			}
		}

		//use ajax to get the material;
		$.post(actionUrl("MaterialInDelivery", "AJAX_GetMaterial"), { "barcode": barcode }, function (res) {
			if (res.Success) {
				var material = $.parseJSON(res.Message);
				_addMaterialRow(material);
			}
			else {
				alert(res.Message);
			}
		});
	});
});

var _orderId = 0;

//id=amount,id=amount,id=amount...
function _serailzeMaterials() {
	var allMaterials = "";
	$.each(_materials, function (i, m) {
		if (typeof (m) != undefined) {
			if (allMaterials.length > 0) allMaterials += ",";
			if (m.amount > 0) {
				allMaterials += m.id + "=" + m.amount;
			} 
		}
	});
	return allMaterials;
}

function submitAllData() {
	var allMaterials = _serailzeMaterials()
	if (allMaterials.length == 0) {
		alert("所有物料的总量为0，不能提交。");
		return;
	}

	//if this value is negative, we need to query all corresponding purchase orders.
	if (_orderId <= 0) {
		//if there are already some <tr> elements.
		if ($("#orderTable tbody tr").length > 0) {
			alert("请选择相应的进货单！");
		}
		else {
			//use ajax to submit all materials;
			$.post(actionUrl("MaterialInDelivery", "AJAX_GetCorrespondingPurchaseOrders"),
			{
				"allMaterials": allMaterials
			},
			function (orders) {
				if (orders.length <= 0) {
					alert("没有相应的进货单。");
					return;
				}

				_removeAllOrders();
				$.each(orders, function (i, o) {
					_addOrderRow(o);
				});
			});
		}
	}
	else {
		if (confirm("确定提交所有的物料到相应的进货单中？")) {
			$("#pConfirm").hide();
			//submit all materials;
			$.post(actionUrl("MaterialInDelivery", "AJAX_SubmitMaterials"),
			{
				"purchaseOrderId": _orderId,
				"allMaterials": allMaterials
			},
			function (res) {
				$("#pConfirm").show();
				alert(res.Message);
				if (res.Success) {
					window.location = actionUrl("MaterialInDelivery", "MaterialInDelivery");
				}
			});
		}
	}
}

function _addMaterialRow(material) {
	var index = _materials.length;
	_materials[index] = material;

	var htmlSegment = '\
<tr>\
	<td>\
		<img src="' + material.image + '" />\
	</td>\
	<td>\
		' + material.barcode + '\
	</td>\
	<td>\
		' + material.name + '\
	</td>\
	<td>\
		<input type="text" id="Amount_' + index + '" value="' + material.amount + '" onchange="_txtAmountChanged(this, ' + index + ')" />\
	</td>\
</tr>\
';

	$("#materialTable tbody").append(htmlSegment);

	$("#warning").hide();
	$("#materialTable").show();
	$("#pConfirm").show();
	altTable($("#materialTable"));
}

function _txtAmountChanged(txt, index) {
	var m = _materials[index];
	var amount = parseInt($(txt).val());
	if (!isNaN(amount)) {
		m.amount = amount;
	}
}

function _addOrderRow(order) {
	var htmlSegment = '\
<tr>\
	<td>\
		<label>\
			<input type="radio" onclick="_setOrder(' + order.id + ')" />选择</label>\
	</td>\
	<td>\
		' + order.barcode + '\
	</td>\
	<td>\
		' + order.createdTime + '\
	</td>\
	<td>\
		' + order.creator + '\
	</td>\
	<td>\
		' + order.amount + '\
	</td>\
</tr>';

	$("#orderTable tbody").append(htmlSegment);
	altTable($("#orderTable"));
	$("#orderTableContainer").show();
}

function _setOrder(id) {
	_orderId = id;
}

function _removeAllOrders() {
	$("#orderTable tbody tr").remove();
	$("#orderTableContainer").hide();
}